﻿using AnesSystem.Models;
using AnesSystem.Services;
using Common;
using MongoDB.Bson;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AnesSystem.ViewModels
{
    class ConfigVitalSignsCtrlViewModel : BaseViewModel<ConfigVitalSignsCtrlViewModel>
    {
        #region Variables
        private string _seachValue = "";

        private ObservableCollection<ConfigVitalSignsModel> _displayConfigVitalSignsModel = new ObservableCollection<ConfigVitalSignsModel>();

        #endregion

        #region Constructor

        public ConfigVitalSignsCtrlViewModel()
        {
            SeachData();
          }
        #endregion

        #region Properties


    
        public ObservableCollection<ConfigVitalSignsModel> DisplayConfigVitalSignsModel
        {
            get
            {
               
                return _displayConfigVitalSignsModel;
            }

            set
            {
                ConfigVitalSignsDataGridViewModel.Instance.ConfigVitalSigns = value;
                RaisePropertyChanged("DisplayConfigVitalSignsModel");
            }
        }

      
        public string SeachValue
        {
            get
            {
                return _seachValue;
            }

            set
            {
                _seachValue = value;
                SeachData();
                RaisePropertyChanged("DisplayConfigVitalSignsModel");
            }
        }

        #endregion

        #region Event

        #endregion

        #region Private Methods
        private void SeachData()
        {
            _displayConfigVitalSignsModel.Clear();
            List<ConfigVitalSignsModel> lst = DataManager.Instance.ConfigVitalSignsModelList.ToList().Where(O => O.name.Contains(_seachValue) || O.code.Contains(_seachValue)).ToList();
            foreach (var item in lst)
            {
                item.PropertyChanged += Item_PropertyChanged;
                _displayConfigVitalSignsModel.Add(item);
            }

        }



        private void Item_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            ConfigVitalSignsModelService.Instance.Update(sender as ConfigVitalSignsModel);
        }

        private void AddConfigVitalSignsModel(ConfigVitalSignsModel obj)
        {
            if (obj.Id != new ObjectId("000000000000000000000000"))
            {
                ConfigVitalSignsModelService.Instance.Update(obj);
                ConfigVitalSignsDataGridViewModel.Instance.ConfigVitalSigns.Add(obj);
            }
            else ConfigVitalSignsModelService.Instance.Insert(obj);


        }

        #endregion
        #region Private Methods
        public void DelConfigVitalSignsModel(ConfigVitalSignsModel configVitalSignsModel)
        {
            if (configVitalSignsModel != null)
            {
                ConfigVitalSignsDataGridViewModel.Instance.ConfigVitalSigns.Remove(configVitalSignsModel);
                RaisePropertyChanged("DisplayConfigVitalSignsModel");
                ConfigUnitTypesModelService.Instance.DeleteById(configVitalSignsModel.Id);
            }
        }

        public void AddRow()
        {
            ConfigVitalSignsModel model = new ConfigVitalSignsModel();
            DisplayConfigVitalSignsModel.Insert(0, model);
            model.PropertyChanged += Model_PropertyChanged;
        }
        #endregion

        #region EventHandlers
        private void Model_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            AddConfigVitalSignsModel(sender as ConfigVitalSignsModel);
        }

        #endregion

        #region Command

        #region ChangeDoctorCommand

        #endregion

        #region OpenPopupCommand

        #endregion
        #endregion
    }
}
